package com.vpx;

import com.googlecode.javacv.cpp.opencv_core;
import java.io.ByteArrayOutputStream;

/**
 * @author sad
 */
public class VpxEncoder extends CodecBase {

    private long codecPointer;
    private long rawVpxImage;
    private long cfg;
    private int flags;
    private final int width;
    private final int height;
    private final int bitrate;
    private final int max_dist;
    private int frame_cnt;

    @SuppressWarnings("LeakingThisInConstructor")
    public VpxEncoder(int width, int height, int bitrate, int max_dist) {
        this.width = width;
        this.height = height;
        this.bitrate = bitrate;
        this.max_dist = max_dist;
        JniVpx.createEncoder(this);
    }

    public static int calculateBasicFrameRate(int width, int height) {
        return width * height * 256 / 320 / 240;
    }

    @Override
    protected void finalize() throws Throwable {
        JniVpx.destroyEncoder(this);
        super.finalize();
    }

    public void encodeImage(final opencv_core.IplImage image, ByteArrayOutputStream stream) {
        long address = getImageAddress(image);
        JniVpx.encodeFrame(address, this, stream);
    }

    public int getMax_dist() {
        return max_dist;
    }

    public long getCodecPointer() {
        return codecPointer;
    }

    public long getRawVpxImage() {
        return rawVpxImage;
    }

    public int getFlags() {
        return flags;
    }

    public int getWidth() {
        return width;
    }

    public int getHeight() {
        return height;
    }

    public int getBitrate() {
        return bitrate;
    }

    public int getFrame_cnt() {
        return frame_cnt;
    }

}
